package study.thread.CriticalSection;

/**
 * @author Ken
 * cannot show the synchronization problem
 * Why?
 */
public class Planner extends Thread {
	private static long resourceStatic;
	private static long resourceNonStatic;
	protected int executeTime;
	protected int id;
//	Thread thread;
	
	protected long getResource(){
		return resourceStatic;
	}
	protected long getNonStaticResource(){
		return resourceNonStatic;
	}
	public Planner(int id, int executeTime){
		this.id=id;
		this.executeTime=executeTime;
		
	}
	
	
//	@Override
	public void run() {
		for (int i=0; i<executeTime; i++ ){
			incrementStatic();	
			incrementNonStatic();
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
//		System.out.println("thread" + id + " resource = " + resource);
	}

	
	public static void incrementStatic(){
		resourceStatic++;
	}
	
	public static void incrementNonStatic(){
		resourceNonStatic++;
	}
	
	/**
	 *main 
	 *@param args
	 */
	public static void main(String[] args) {
		int objCount=3000;
		Planner[] planners = new Planner[objCount];
		for (int i=0; i<objCount; i++){
			planners[i]=new Planner(i,1000);
			planners[i].start();
		}
		for (int i=0; i<objCount; i++){
			try {
				planners[i].join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println ("Static: " + planners[0].getResource());
		System.out.println ("Non Static: " + planners[0].getResource());
		System.exit(0);
	}
	
	
	
//	protected void start(){
////		thread.start();
//		run();
//	}
	

}
